package org.jeecg.modules.golf.tag.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.golf.question.client.QuestionTagAnalysisDTO;
import org.jeecg.modules.golf.question.client.qto.StatisticsQuestionAnalysisQueryDTO;
import org.jeecg.modules.golf.tag.entity.RelationTag;
import org.jeecg.modules.golf.tag.entity.TagMapping;
import org.jeecg.modules.golf.tag.mapper.RelationTagMapper;
import org.jeecg.modules.golf.tag.mapper.TagMappingMapper;
import org.jeecg.modules.golf.tag.service.ITagMappingService;
import org.jeecg.modules.golf.tag.service.ITagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 标签关联表
 * @Author: jeecg-boot
 * @Date:   2020-10-18
 * @Version: V1.0
 */
@Service
public class TagMappingServiceImpl extends ServiceImpl<TagMappingMapper, TagMapping> implements ITagMappingService {

	@Resource
	private ITagService tagService;


	@Resource
	private RelationTagMapper relationTagMapper;





	@Override
	public List<TagMapping> selectByMainId(Long mainId,Integer mappingType) {
		return getBaseMapper().selectByMainId(mainId,mappingType);
	}

	@Override
	public List<Map<String, String>> queryTagByGroup(String userid) {
		return getBaseMapper().queryTagByGroup(userid);
	}

	/**
	 * 批量保存并更新Tag
	 *
	 * @param tagMappingList
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveBatch(List<TagMapping> tagMappingList,Long mainId,Integer mappingType) {
		getBaseMapper().deleteByMainId(mainId);
		List<TagMapping> tagList = new ArrayList<>();


		if(tagMappingList!=null && tagMappingList.size()>0) {
			for(TagMapping entity:tagMappingList) {
				entity.setTagId(tagService.findTagIdByTagName(entity.getTagName()));
				//外键设置
				entity.setMappingId(mainId);
				entity.setMappingType(mappingType);
				tagList.add(entity);
			}
		}
		saveBatch(tagMappingList);
		saveRelationTag(mappingType, tagList);


	}

	/**
	 * 保存相关标签
	 * @param mappingType
	 * @param tagList
	 */
	private void saveRelationTag( Integer mappingType, List<TagMapping> tagList) {
		if(mappingType == 1){
			List<RelationTag> relationTagList = new ArrayList<>(tagList.size()*2);


			for(int i=0;i< tagList.size();i++){

				for(int j=0;j<tagList.size();j++){
					if(i == j){
						continue;
					}
					RelationTag relationTagPO = new RelationTag();
					TagMapping tagMapping = tagList.get(i);
					relationTagPO.setTagId(tagMapping.getTagId());
					relationTagPO.setTagName(tagMapping.getTagName());
					TagMapping relationTag = tagList.get(j);
					relationTagPO.setRelationTagId(relationTag.getTagId());
					relationTagPO.setRelationTagName(relationTag.getTagName());
					relationTagList.add(relationTagPO);

				}
			}
			if(CollectionUtils.isNotEmpty(relationTagList)){
				relationTagMapper.batchInsertOrUpdate(relationTagList);
			}
		}
	}


	@Override
	public Page<QuestionTagAnalysisDTO> tagAnalysis(StatisticsQuestionAnalysisQueryDTO statisticsQuestionAnalysisQueryDTO){
		return getBaseMapper().tagAnalysis(statisticsQuestionAnalysisQueryDTO);
	}


}
